<?php
namespace app\api\model;

use think\Model;

class Goods extends Base{
    protected $name = 'goods';
    protected $updateTime = false;

    // 商品详情
    public function getGoodsShow($goods_id, $field = '*')
    {
        try{
            $res = $this->where(['id' => $goods_id])->field($field)->find();
            
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                $goods_img_list = (new GoodsImg())->getList(['goods_id' => $res['id'], 'goods_type' => 1], 0, 0, 0, 'id, img, type', 'sort');

                if($goods_img_list['code'] != 'ok'){
                    $res['img'] = [];
                }else{
                    $res['img'] = $goods_img_list['data'];
                }

                return ['code' => 'ok','data' => $res];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    // 获取器
    public function getSmallImgAttr($val)
    {
        if(!$val){
            return $val;
        }

        return config('app.url') . $val;
    }
}